﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	The GNU Library General Public License (LGPL) used in RapidWebDev is 
	intended to guarantee your freedom to share and change free software - to 
	make sure the software is free for all its users.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License (LGPL) for more details.

	You should have received a copy of the GNU Library General Public License (LGPL)
	along with this program.  
	If not, see http://www.rapidwebdev.org/Content/ByUniqueKey/OpenSourceLicense
 ****************************************************************************************************/

using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using RapidWebDev.Common;

namespace RapidWebDev.FileManagement.Thumbnails
{
    /// <summary>
    /// Thumbnail processor interface.
    /// </summary>
    public class ImageThumbnailProcessor : IThumbnailProcessor
    {
        /// <summary>
        /// Generage thumbnail for specified file and return path of thumbnail file on the server.
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="thumbnailFilePath"></param>
        /// <param name="thumbnailSize"></param>
        /// <returns>return path of thumbnail file on the server.</returns>
        public void GenerateThumbnail(Stream fileStream, string thumbnailFilePath, Size thumbnailSize)
        {
            using (Image originalImage = Image.FromStream(fileStream))
            {
                Size actualThumbnailSize = ThumbnailUtilities.CalculateActualThumbnailSize(originalImage.Size, thumbnailSize);

                using (Image sourceImage = originalImage.GetThumbnailImage(actualThumbnailSize.Width, actualThumbnailSize.Height, null, IntPtr.Zero))
                using (Image thumbnailImage = ThumbnailUtilities.ResizeThumbnailImage(sourceImage, actualThumbnailSize, thumbnailSize))
                {
                    ThumbnailUtilities.SaveImageToFile(thumbnailImage, thumbnailFilePath);
                }
            }
        }
    }
}